﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlatRedBall.Input;
using Microsoft.Xna.Framework.Input;

namespace Gnome.Engine
{
    class CustomInputManager
    {
        public static float LEFT_STICK_DEADZONE = 0.2f;
        public static Xbox360GamePad.Button HIDE_BUTTON = Xbox360GamePad.Button.A;
        public static Xbox360GamePad.Button DROP_BUTTON = Xbox360GamePad.Button.X;
        
        public static Boolean isLeftStickUp(Xbox360GamePad gamepad)
        {
            if (!gamepad.IsConnected)
                return false;
            if (gamepad.LeftStick.Angle >= Math.PI / 4 && gamepad.LeftStick.Angle < 3 * Math.PI / 4
                && gamepad.LeftStick.Magnitude > LEFT_STICK_DEADZONE)
                return true;
            else
                return false;
        }

        public static Boolean isLeftStickDown(Xbox360GamePad gamepad)
        {
            if (!gamepad.IsConnected)
                return false;
            if (gamepad.LeftStick.Angle <= -1 * Math.PI / 4 && gamepad.LeftStick.Angle > -3 * Math.PI / 4
                && gamepad.LeftStick.Magnitude > LEFT_STICK_DEADZONE)
                return true;
            else
                return false;
        }

        public static Boolean isLeftStickLeft(Xbox360GamePad gamepad)
        {
            if (!gamepad.IsConnected)
                return false;
            if ((gamepad.LeftStick.Angle >= 3 * Math.PI / 4 || gamepad.LeftStick.Angle < -3 * Math.PI / 4)
                && gamepad.LeftStick.Magnitude > LEFT_STICK_DEADZONE)
                return true;
            else
                return false;
        }

        public static Boolean isLeftStickRight(Xbox360GamePad gamepad)
        {
            if (!gamepad.IsConnected)
                return false;
            if (gamepad.LeftStick.Angle <= Math.PI / 4 && gamepad.LeftStick.Angle > -1 * Math.PI / 4
                && gamepad.LeftStick.Magnitude > LEFT_STICK_DEADZONE)
                return true;
            else
                return false;
        }

        public static Boolean isCharacterMoveUp(Xbox360GamePad gamepad, Boolean isFirstPlayer)
        {
            if (GlobalData.is2P)
            {
                if (isFirstPlayer)
                    return isLeftStickUp(gamepad) || InputManager.Keyboard.KeyDown(Keys.Up);
                else
                    return isLeftStickUp(gamepad) || InputManager.Keyboard.KeyDown(Keys.W);
            }
            else
                return isLeftStickUp(gamepad) || InputManager.Keyboard.KeyDown(Keys.W) || InputManager.Keyboard.KeyDown(Keys.Up);
        }

        public static Boolean isCharacterMoveDown(Xbox360GamePad gamepad, Boolean isFirstPlayer)
        {
            if (GlobalData.is2P)
            {
                if (isFirstPlayer)
                    return isLeftStickDown(gamepad) || InputManager.Keyboard.KeyDown(Keys.Down);
                else
                    return isLeftStickDown(gamepad) || InputManager.Keyboard.KeyDown(Keys.S);
            }
            else
                return isLeftStickDown(gamepad) || InputManager.Keyboard.KeyDown(Keys.S) || InputManager.Keyboard.KeyDown(Keys.Down);
        }

        public static Boolean isCharacterMoveLeft(Xbox360GamePad gamepad, Boolean isFirstPlayer)
        {
            if (GlobalData.is2P)
            {
                if (isFirstPlayer)
                    return isLeftStickLeft(gamepad) || InputManager.Keyboard.KeyDown(Keys.Left);
                else
                    return isLeftStickLeft(gamepad) || InputManager.Keyboard.KeyDown(Keys.A);
            }
            else
                return isLeftStickLeft(gamepad) || InputManager.Keyboard.KeyDown(Keys.A) || InputManager.Keyboard.KeyDown(Keys.Left);
        }

        public static Boolean isCharacterMoveRight(Xbox360GamePad gamepad, Boolean isFirstPlayer)
        {
            if (GlobalData.is2P)
            {
                if (isFirstPlayer)
                    return isLeftStickRight(gamepad) || InputManager.Keyboard.KeyDown(Keys.Right);
                else
                    return isLeftStickRight(gamepad) || InputManager.Keyboard.KeyDown(Keys.D);
            }
            else
                return isLeftStickRight(gamepad) || InputManager.Keyboard.KeyDown(Keys.D) || InputManager.Keyboard.KeyDown(Keys.Right);
        }

        public static Boolean isHideButtonPushed(bool isFirstPlayer)
        {
            if (isFirstPlayer)
            {
                if (InputManager.Keyboard.KeyPushed(Keys.Space) || InputManager.Xbox360GamePads[0].ButtonPushed(HIDE_BUTTON))
                    return true;
                else
                    return false;
            }
            else
            {
                if (InputManager.Keyboard.KeyPushed(Keys.LeftShift) || InputManager.Xbox360GamePads[1].ButtonPushed(HIDE_BUTTON))
                    return true;
                else
                    return false;
            }
        }

        public static Boolean isDropButtonPushed(bool isFirstPlayer)
        {
            if (isFirstPlayer)
            {
                if (InputManager.Keyboard.KeyPushed(Keys.RightControl) || InputManager.Xbox360GamePads[0].ButtonPushed(DROP_BUTTON))
                    return true;
                else
                    return false;
            }
            else
            {
                if (InputManager.Keyboard.KeyPushed(Keys.LeftControl) || InputManager.Xbox360GamePads[1].ButtonPushed(DROP_BUTTON))
                    return true;
                else
                    return false;
            }
        }

        public static Boolean isPauseButtonPushed(bool isPaused)
        {
            if (InputManager.Keyboard.KeyPushed(Keys.Escape) || InputManager.Xbox360GamePads[0].ButtonPushed(Xbox360GamePad.Button.Start))
                return true;
            if ((InputManager.Keyboard.KeyPushed(Keys.Enter) || InputManager.Keyboard.KeyPushed(Keys.Space) ||
                InputManager.Xbox360GamePads[0].ButtonPushed(Xbox360GamePad.Button.B) || InputManager.Xbox360GamePads[0].ButtonPushed(Xbox360GamePad.Button.Back) ||
                InputManager.Xbox360GamePads[0].ButtonPushed(Xbox360GamePad.Button.A)) && isPaused)
                return true;
            if (GlobalData.is2P)
            {
                if (InputManager.Xbox360GamePads[1].ButtonPushed(Xbox360GamePad.Button.Start))
                    return true;
                if ((InputManager.Xbox360GamePads[1].ButtonPushed(Xbox360GamePad.Button.B) || InputManager.Xbox360GamePads[1].ButtonPushed(Xbox360GamePad.Button.Back) ||
                    InputManager.Xbox360GamePads[1].ButtonPushed(Xbox360GamePad.Button.A)) && isPaused)
                    return true;
            }
            return false;
        }



        public static bool isMenuUp()
        {
            if (InputManager.Keyboard.KeyPushed(Keys.Up) || InputManager.Keyboard.KeyPushed(Keys.W))
                return true;
            if (InputManager.Xbox360GamePads[0].LeftStick.AsDPadPushed(Xbox360GamePad.DPadDirection.Up) ||
                InputManager.Xbox360GamePads[0].ButtonPushed(Xbox360GamePad.Button.DPadUp))
                return true;
            if ((InputManager.Xbox360GamePads[1].LeftStick.AsDPadPushed(Xbox360GamePad.DPadDirection.Up) ||
                InputManager.Xbox360GamePads[1].ButtonPushed(Xbox360GamePad.Button.DPadUp)) && GlobalData.is2P)
                return true;
            return false;
        }

        public static bool isMenuDown()
        {
            if (InputManager.Keyboard.KeyPushed(Keys.Down) || InputManager.Keyboard.KeyPushed(Keys.S))
                return true;
            if (InputManager.Xbox360GamePads[0].LeftStick.AsDPadPushed(Xbox360GamePad.DPadDirection.Down) ||
                InputManager.Xbox360GamePads[0].ButtonPushed(Xbox360GamePad.Button.DPadDown))
                return true;
            if ((InputManager.Xbox360GamePads[1].LeftStick.AsDPadPushed(Xbox360GamePad.DPadDirection.Down) ||
                InputManager.Xbox360GamePads[1].ButtonPushed(Xbox360GamePad.Button.DPadDown)) && GlobalData.is2P)
                return true;
            return false;
        }

        public static bool isMenuLeft()
        {
            if (InputManager.Keyboard.KeyPushed(Keys.Left) || InputManager.Keyboard.KeyPushed(Keys.A))
                return true;
            if (InputManager.Xbox360GamePads[0].LeftStick.AsDPadPushed(Xbox360GamePad.DPadDirection.Left) ||
                InputManager.Xbox360GamePads[0].ButtonPushed(Xbox360GamePad.Button.DPadLeft))
                return true;
            if ((InputManager.Xbox360GamePads[1].LeftStick.AsDPadPushed(Xbox360GamePad.DPadDirection.Left) ||
                InputManager.Xbox360GamePads[1].ButtonPushed(Xbox360GamePad.Button.DPadLeft)) && GlobalData.is2P)
                return true;
            return false;
        }

        public static bool isMenuRight()
        {
            if (InputManager.Keyboard.KeyPushed(Keys.Right) || InputManager.Keyboard.KeyPushed(Keys.D))
                return true;
            if (InputManager.Xbox360GamePads[0].LeftStick.AsDPadPushed(Xbox360GamePad.DPadDirection.Right) ||
                InputManager.Xbox360GamePads[0].ButtonPushed(Xbox360GamePad.Button.DPadRight))
                return true;
            if ((InputManager.Xbox360GamePads[1].LeftStick.AsDPadPushed(Xbox360GamePad.DPadDirection.Right) ||
                InputManager.Xbox360GamePads[1].ButtonPushed(Xbox360GamePad.Button.DPadRight)) && GlobalData.is2P)
                return true;
            return false;
        }

        public static bool isMenuSelect()
        {
            if (InputManager.Keyboard.KeyPushed(Keys.Enter) || InputManager.Keyboard.KeyPushed(Keys.Space))
                return true;
            if (InputManager.Xbox360GamePads[0].ButtonPushed(Xbox360GamePad.Button.A) ||
                InputManager.Xbox360GamePads[0].ButtonPushed(Xbox360GamePad.Button.Start))
                return true;
            if ((InputManager.Xbox360GamePads[1].ButtonPushed(Xbox360GamePad.Button.A) ||
                InputManager.Xbox360GamePads[1].ButtonPushed(Xbox360GamePad.Button.Start)) && GlobalData.is2P)
                return true;
            return false;
        }

        public static bool isMenuCancel()
        {
            if (InputManager.Keyboard.KeyPushed(Keys.Escape))
                return true;
            if (InputManager.Xbox360GamePads[0].ButtonPushed(Xbox360GamePad.Button.B) ||
                InputManager.Xbox360GamePads[0].ButtonPushed(Xbox360GamePad.Button.Back))
                return true;
            if ((InputManager.Xbox360GamePads[1].ButtonPushed(Xbox360GamePad.Button.B) ||
                InputManager.Xbox360GamePads[1].ButtonPushed(Xbox360GamePad.Button.Back)) && GlobalData.is2P)
                return true;
            return false;
        }
    }
}
